//////////////////////////////////////////////////////////////////////////////////////////////////////////
//																										//
//																										//
//                                                                                                      //
//																										//
//          VEHICLE REGISTRATION SYSTEM	PROGRAM                                                         //
//											 															//
//																										//
//            Group Members:																			//
//                 M Ali																		//
//				   Saad Abdullah Khan																			//
//				   Malik Furqan Naseer																	//
//																										//
//////////////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////Header Files/////////////////////

#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>


using namespace std;



int record_counter=0;



//////////////////base class starts//////////////


class Registration                      
{
	friend void operator<<(ostream &out,Registration &a);
public:
	int reg_no;
	char vtype[20];					//type of vehicle
	char compname[20];//name of company
	char province[10];
	char city[10];
	int modelno;						//model number
	int chasis;						//chaisis no	
	char nameowner[20];                //owner name
	char  dateissue[20];               //issued date
	

	Registration()                //default Constructor Defined
 {
	 modelno=0;
	 reg_no=0;
	 
	 
	 for (int i=0;i<20;i++)
	 {
		 vtype[i]=0;
		 compname[i]=0;
		 nameowner[i]=0;
		 dateissue[i]=0;
		 province[10]=0;
		 city[10]=0;
	 }
 }

	////////////"<< opearor overloading definition/////////////////////


	friend void operator<<(ostream &out,Registration &a)  //operator overloading def of << operator
{
 
	out<<a.reg_no<<"	"<<a.vtype<<"	"<<a.compname<<"	"<<a.modelno<<"	"<<a.chasis<<" 	"<<a.nameowner<<"  	"<<a.dateissue<<" 	 "<<a.province<<" 	"<<a.city<<"\n";
}

	

	 virtual void database()
		
	{

		cout<<"You are about to add new record in the database."<<"\n\n\n\n\n"<<endl;
		cout<<"Enter the registration number of the vehicle:"<<endl;
		cin>>reg_no;
		cout<<"Enter vehicle type:"<<endl;
		cin>>vtype;
		cout<<"Enter name of the company:"<<endl;
		cin>>compname;
		cout<<"Enter model number:"<<endl;
		cin>>modelno;
		
		cout<<"Enter the chasis number of the vehicle:"<<endl;
		cin>>chasis;
		
		cout<<"Enter the name of the owner of car:"<<endl;
		cin>>nameowner;
		cout<<"Enter the date of issue:"<<endl;
		cin>>dateissue;
		cout<<"enter province name";
        cin>>province;
		cout<<"enter the city name";
		cin>>city;
	
	
		
	}



  
	

    int Password(string password)                 //password function defined
	   {
		   string password1=password;
		   
		   if(password1=="****")
		   {
			   return 1;
		   }
		   else 
			   return 0;
	   }


};

///////////////////////////////////////base class ends///////////////////////////




///////////////////////////////////Derived class Edit starts/////////////////////


class Edit:public Registration                          //derived publically
{
public:
	
	virtual void database()
	{
		cout<<"Updating database wizard...."<<"\n\n\n\n\n"<<endl;
		cout<<"Enter the registration number of the vehicle:"<<endl;
		cin>>reg_no;
		cout<<"Enter vehicle type:"<<endl;
		cin>>vtype;
		cout<<"Enter name of the company:"<<endl;
		cin>>compname;
		cout<<"Enter model number:"<<endl;
		cin>>modelno;
		
		cout<<"Enter the chasis number of the vehicle:"<<endl;
		cin>>chasis;
		
		cout<<"Enter the name of the owner of car:"<<endl;
		cin>>nameowner;
		cout<<"Enter the date of issue:"<<endl;
		cin>>dateissue;
		cout<<"enter province name";
        cin>>province;
		cout<<"enter the city name";
		cin>>city;

	}
	  

};  

////////////////derived class Edit ends///////////////////





/////////////////////////////main starts//////////////////


	void   main()
	{
		int position=0;int reg=0;int i=0;int size=0;int b;
		int check=0;
		string password;

		Registration a;

		Edit e;                             //derived class edit

		Registration *eptr=&e; 
		Registration *ptr=&a;// sizeof(Registration);
				 //base class pointer declared (polymorphism)

		ifstream infile;
		ofstream outfile;
		

		////////////block to allocate space on file////////////////

	    /*outfile.open("database.txt",ios::binary);

		for( i=0;i<200;i++)
				  {
				  outfile.write(( char*)(&a),sizeof(Registration));
				  }
	    outfile.close();*/





		//////////////////////exception handling//////////////////




		try                //try block starts
		{ 
			int c=0;
			cout<<"//////////////////////////\n";
			cout<<"///Enter your password:///\n";
            cout<<"//////////////////////////\n";
			cin>>password;
			c=a.Password(password);
			if(c!=1)
			{

                cout<<"Exceptoion occured....\n\n"<<endl;
		        cout<<"Wrong password\n\n";
		        throw(password);                                 //if exeption occured then throw in throw component

			}
		}                    //try block ends


		
        //catch block starts

		catch(string password)            //catch block catching the exeption
		   {
			     cout<<"Enter your password again..."<<endl;
			     while(password!="ali")
			   {
				  cin>>password;
				  
			   }
				 
		}                                //catch block ends

	  
	    
		       
		cout<<"**************************************************"<<endl;
		cout<<"   WELCOME TO VEHICLE REGISTRAION SYSTEM PROGRAM   "<<endl;
		cout<<"**************************************************"<<endl;
		
		char ch='y';
		int input;
		do
		{

		
	            cout<<"What you want to do...\n\n\n"<<endl;
		        cout<<"For viewing database press:1"<<endl;
		        cout<<"For searching a record press:2"<<endl;
		        cout<<"For adding new record press:3"<<endl;
		        cout<<"To edit a record press:4"<<endl;
				cout<<"To delete a record press:5"<<endl;
			    cin>>input;
			switch(input)
			{


				////////////display database case///////////////


		case 1:
			    infile.open("database.txt",ios::binary);
				if(!infile)
				{
					cout<<"File does not exist\n\n";
					break;
				}
				else
	            cout<<"you requested for viewing database.\n\n\n"<<endl;
		        cout<<"........Displaying database......"<<"\n\n\n";
		        cout<<"***************************************************************************"<<endl;
		        cout<<"reg.no"<<" "<<"vcle.type"<<" "<<"company"<<" "<<"model"<<" "<<"chasis"<<" "<<"owner"<<" "<<"issue.date"<<" "<<" province"<<"    "<<"city"<<"      "<<endl;
		        cout<<"***************************************************************************"<<endl;
		 
              

				 
				
				infile.seekg(0);                                  //seekg taking at start of file
				infile.read(( char * )( &a ), sizeof(Registration ) );  
          i=-1;
          while(infile.good())
		  {              
                
                   cout<<a; 
				infile.read(( char * )( &a ), sizeof(Registration ) );  
		             
		  }
		  cout<<"\n\n\n";
		  
		  //infile.seekg(0);
			  
			    infile.close();
	
	
             break;


         /////////////////////search a record case/////////////


		case 2:  
			    infile.open("database.txt",ios::binary);
				 	if(!infile)
				{
					cout<<"File does not exist\n\n";
					break;
				}
				else
	             
                 cout<<"///////////////////////////////////////"<<endl;
				 cout<<"you requested for searching a vehicle//"<<endl;
				 cout<<"///////////////////////////////////////"<<"\n\n\n"<<endl;
				 cout<<"\n\n\nEnter the registration no of vehicle to be searched:";
		         cin>>reg;
				  
                 cout<<"\n\n\n Searching record...."<<endl;
				 position=sizeof(Registration);
			     
		i=-1;
		while(i!=record_counter+1)
		{
			infile.read((char*)(&a),sizeof(Registration));
			
	
         if(reg==a.reg_no)
		{
			 

			     cout<<"\n\n\n record found...."<<"\n\n\n\n"<<endl;
			     
			     cout<<"*******************************************************************************"<<endl;
		         cout<<"reg_no"<<"    "<<"vehicle_type"<<"   "<<"company"<<"   "<<"model"<<"   "<<"chaisis_no"<<"     "<<"owner"<<"    "<<"issuance_date"<<endl;
		         cout<<"*******************************************************************************"<<endl;
			     cout<<a;
			     cout<<"\n\n\n\n";
				 b=1;
				 infile.close();
		         break;
		
			
		}
		
		
        else 
			     
		{       infile.read((char*)(&a),sizeof(Registration));
			    position+=sizeof(Registration);
			    infile.seekg(position,ios::beg);
				i++;
				
		            
		}	 
		
		}
		if(b==1)
		{infile.close();}
		else
		{cout<<"Record not found"<<"\n\n";
		infile.close();
		}
		
		break;


            //////////////add new record case//////////////////


		case 3:
					
		          
		          cout<<"you requested to add new record."<<endl;
				  infile.open("database.txt",ios::binary);       //file opening in binary mode for reading
				  a.database();
				  reg=a.reg_no;
				  i=-1;
				  
				  while(!infile.good())
				  {
			          infile.read((char*)(&a),sizeof(Registration));
		
                   if(reg==a.reg_no)
				   {
					   check=1;
					   infile.close();
					   break;
				   }
					   else 
			     
					   {
			             position+=sizeof(Registration);
			             infile.seekg(position,ios::beg);
		                 i++;
					   }
				  }
			             if(check==1)
						 {
							 cout<<"registration no collides with another entry....please try another\n\n\n";
							 break;
						 }
						 else
							 infile.close();
							 outfile.open("database.txt",ios::out|ios::ate|ios::app|ios::binary);             //file opening for writing in binary mode
				  outfile.write(( const char *)(&a),sizeof(Registration));
		          cout<<"///////////////////////////////////\n";
				  cout<<"Values have been written to file.//\n";
				  cout<<"///////////////////////////////////\n";
				  outfile.close();
				  
		         
				  break;

              ////////////////edit a record case///////////////


		case 4: 
			     
			      cout<<"////////////////////////////////////\n";
				  cout<<"//you requested to edit the record//\n";
				  cout<<"////////////////////////////////////\n\n\n";
				   cout<<"\n\n Enter the registration number of the record whom you want to edit:";
			cin>>reg;
			 i=-1;
			//ifstream infile;                               //ifstream object
			infile.open("database.txt",ios::binary);       //file opening in binary mode for reading
		     position=sizeof(Registration);
			 infile.seekg(0);
             while(!infile.eof())
				  {
			          infile.read((char*)(&a),sizeof(Registration));
		
                   if(reg==a.reg_no)
				   {
					   check=1;
					   break;
				   }
					   else 
			     
					   {
			             position+=sizeof(Registration);
			             infile.seekg(position,ios::beg);
		                 i++;
					   }
			 }
			 
			
			
	
	            position-=sizeof(Registration);
				
				infile.close();                            //file closing
                  
				  
		        
				  if(check==1)
				  {

			      cout<<"\n\n\n one match found...."<<"\n\n\n\n"<<endl;
				  a.database();                                    //polymorphically call
				  outfile.open("database.txt",ios::out|ios::binary);
				            //getting position
				  outfile.seekp(position,ios::beg);                             //seeking position for writing
				  
		         

			      outfile.write((const  char *)(&a),sizeof(Registration));
				  outfile.close();
			      cout<<"/////////////////////////////////////\n";
			      cout<<"// Database has updated your record//\n";
			      cout<<"/////////////////////////////////////\n";
                  break;
				 }
			    	else  
					{
						cout<<"registration number did not match"<<endl;
					}
					

					break;
				  


		/*case 5:
			cout<<"reg ??"<<endl;
			cin>>reg;
			infile.open("database.txt",ios::binary);       //file opening in binary mode for reading
		     position=sizeof(Registration);
			 i=-1;
             while(i!=record_counter)
				  {
			          infile.read((char*)(&a),sizeof(Registration));
		
                   if(reg==a.reg_no)
				   {
					   check=1;
					   break;
				   }
					   else 
			     
					   {
			             position+=sizeof(Registration);
						 
			             infile.seekg(position,ios::beg);
		                 i++;
					   }
			 }
			 if(check==1)
			 {
				 
				 delete ptr;
				 cout<<"entry deleted"<<endl;
				 break;
			 }
			 else 
			 {
				 cout<<"file not found"<<endl;
			 }
			 infile.close();
			 

				  break;*/
				
			      default:
				  cout<<"invalid entry...\n\n"<<endl;
				  cout<<"programme is exiting......."<<endl;
				 }
		
		
		        
			
		          cout<<"If you want to carry on press (y) or press any other key to exit:"<<endl;
		          cin>>ch;
				  
				 
				  }
		          while(ch=='y');
		          cout<<"/////////////////////////////////////////////////"<<endl;
		          cout<<"//..................good bye..................//"<<endl;
		          cout<<"/////////////////////////////////////////////////"<<endl;
				 				   
				  }



				  /////////////////main ends////////////////////
				 
				 
	
	

		   
		   








